Passed
Pull Request — master (#20)
by Muhammad Dyas
01:43
created

NewPollFormCard.buildFooter   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
1
import BaseCard from './BaseCard';
2
import {ClosableType, PollForm} from '../helpers/interfaces';
3
import {MAX_NUM_OF_OPTIONS} from '../config/default';
4
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
5
import {offsetToTimezone} from '../helpers/time';
6
7
export default class NewPollFormCard extends BaseCard {
8
  private config: PollForm;
9
  private timezone: chatV1.Schema$TimeZone;
10
11
  constructor(config: PollForm, timezone: chatV1.Schema$TimeZone | undefined = undefined) {
12
    super();
13
    this.config = config;
14
    if (timezone?.offset) {
15
      this.timezone = timezone;
16
    } else {
17
      this.timezone = {offset: 0, id: 'GMT'};
18
    }
19
  }
20
21
  create() {
22
    this.buildSections();
23
    this.buildFooter();
24
    return this.card;
25
  }
26
27
  buildSections() {
28
    this.buildTopicInputSection();
29
    this.buildOptionSwitchSection();
30
    this.buildCloseConfigSection();
31
    if (this.config.autoClose) {
32
      this.buildAutoCloseSection();
33
    }
34
  }
35
36
  buildTopicInputSection() {
37
    const widgets = [];
38
    widgets.push(this.buildHelpText());
39
    widgets.push(this.topicInput(this.config.topic));
40
    for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
41
      const choice = this.config.choices?.[i];
42
      widgets.push(this.optionInput(i, choice));
43
    }
44
    this.card.sections!.push({
45
      'collapsible': true,
46
      'uncollapsibleWidgetsCount': 6,
47
      widgets,
48
    });
49
  }
50
51
  buildOptionSwitchSection() {
52
    this.card.sections!.push({
53
      'widgets': [
54
        {
55
          'decoratedText': {
56
            'bottomLabel': 'If this checked the voters name will be not shown',
57
            'text': 'Anonymous voter',
58
            'switchControl': {
59
              'controlType': 'SWITCH',
60
              'name': 'is_anonymous',
61
              'value': '1',
62
              'selected': this.config.anon ?? false,
63
            },
64
          },
65
          'horizontalAlignment': 'CENTER',
66
        },
67
        {
68
          'decoratedText': {
69
            'bottomLabel': 'After the poll is created, other member can add more option',
70
            'text': 'Allow to add more option(s)',
71
            'switchControl': {
72
              'controlType': 'SWITCH',
73
              'name': 'allow_add_option',
74
              'value': '1',
75
              'selected': this.config.optionable ?? true,
76
            },
77
          },
78
          'horizontalAlignment': 'CENTER',
79
        },
80
      ],
81
    });
82
  }
83
84
  buildCloseConfigSection() {
85
    const widgets: chatV1.Schema$GoogleAppsCardV1Widget[] = [
86
      {
87
        'selectionInput': {
88
          'type': 'DROPDOWN',
89
          'label': 'Allow to manually close poll',
90
          'name': 'type',
91
          'items': [
92
            {
93
              'text': 'Yes, but only creator',
94
              'value': '1',
95
              'selected': this.config.type === ClosableType.CLOSEABLE_BY_CREATOR,
96
            },
97
            {
98
              'text': 'Yes, anyone can close',
99
              'value': '2',
100
              'selected': this.config.type === ClosableType.CLOSEABLE_BY_ANYONE,
101
            },
102
            {
103
              'text': 'No, I want unclosable poll',
104
              'value': '0',
105
              'selected': this.config.type === ClosableType.UNCLOSEABLE,
106
            },
107
          ],
108
        },
109
        'horizontalAlignment': 'START',
110
      },
111
      {
112
        'decoratedText': {
113
          'topLabel': '',
114
          'text': 'Automatic close poll at certain time',
115
          'bottomLabel': 'The schedule time will show up',
116
          'switchControl': {
117
            'controlType': 'SWITCH',
118
            'name': 'is_autoclose',
119
            'value': '1',
120
            'selected': this.config.autoClose ?? false,
121
            'onChangeAction': {
122
              'function': 'new_poll_on_change',
123
              'parameters': [],
124
            },
125
          },
126
        },
127
      }];
128
    this.card.sections!.push({
129
      widgets,
130
    });
131
  }
132
133
  buildAutoCloseSection() {
134
    const widgets: chatV1.Schema$GoogleAppsCardV1Widget[] = [];
135
    const timezone = offsetToTimezone(this.timezone.offset!);
136
    const nowMs = Date.now() + this.timezone.offset! + 18000000;
137
    widgets.push(
138
      {
139
        'dateTimePicker': {
140
          'label': 'Close schedule time ' + timezone,
141
          'name': 'close_schedule_time',
142
          'type': 'DATE_AND_TIME',
143
          'valueMsEpoch': nowMs.toString(),
144
        },
145
      });
146
147
    widgets.push(
148
      {
149
        'decoratedText': {
150
          'text': 'Auto mention <b>@all</b> on 5 minutes before poll closed',
151
          'bottomLabel': 'This is to prevent other users to vote before the poll is closed',
152
          'switchControl': {
153
            'controlType': 'SWITCH',
154
            'name': 'auto_mention',
155
            'value': '1',
156
            'selected': this.config.autoMention ?? false,
157
          },
158
        },
159
      });
160
    this.card.sections!.push({
161
      widgets,
162
    });
163
  }
164
165
  buildHelpText() {
166
    return {
167
      textParagraph: {
168
        text: 'Enter the poll topic and up to 10 choices in the poll. Blank options will be omitted.<br>' +
169
          'For scheduled auto close, the minimum time is 5 minutes after poll created.',
170
      },
171
    };
172
  }
173
174
  topicInput(topic: string) {
175
    return {
176
      textInput: {
177
        label: 'Topic',
178
        type: 'MULTIPLE_LINE',
179
        name: 'topic',
180
        value: topic,
181
      },
182
    };
183
  }
184
185
  optionInput(
186
    index: number, value: string): chatV1.Schema$GoogleAppsCardV1Widget {
187
    return {
188
      textInput: {
189
        label: `Option ${index + 1}`,
190
        type: 'SINGLE_LINE',
191
        name: `option${index}`,
192
        value: value || '',
193
      },
194
    };
195
  }
196
197
  buildFooter() {
198
    this.card.fixedFooter = {
199
      'primaryButton': this.createButton('Submit', 'start_poll'),
200
    };
201
  }
202
}
203